# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def breadcrumb_for_category(category)
    breadcrumb = "<a href='/'>首页</a>"

    if category
      category.ancestors().reverse!.each do |c|
        breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.name}</a>"
      end
      breadcrumb += ">#{category.name}"
    end

    return breadcrumb
  end

  def breadcrumb_for_product(product)
    breadcrumb = "<a href='/'>首页</a>"

    if product
      product.category.ancestors().reverse.each do |c|
        breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.name}</a>"
      end
      breadcrumb += "><a href='/categories/#{product.category.id.to_s}'>#{product.category.name}</a>>#{product.name}"
    end

    return breadcrumb
  end

  def is_admin(user_id)
    user = user_id &&  User.find(user_id)
    if user
      user.has_role?('administrator')
    else
      false
    end
  end

  def use_tinymce
    @content_for_tinymce = ""
    content_for :tinymce do
      javascript_include_tag "tiny_mce/tiny_mce"
    end
    @content_for_tinymce_init = ""
    content_for :tinymce_init do
      javascript_include_tag "mce_editor"
    end
  end

  def is_index?
    action_name == 'index' && controller.controller_name == 'store'
  end

  def is_for_him?
    action_name == 'for_him' && controller.controller_name == 'store'
  end
  
  def is_for_her?
    action_name == 'for_her' && controller.controller_name == 'store'
  end
  
  def is_designs?
    action_name == 'index' && controller.controller_name == 'designs'
  end
  
  def is_styles?
    action_name == 'materials' && controller.controller_name == 'store'
  end
  
  def is_products?
    action_name == 'index' && controller.controller_name == 'products'
  end
  
  def is_mine?
    (action_name == 'mine' && controller.controller_name == 'store') or
      (action_name == 'show' && controller.controller_name == 'orders')
  end

  def is_mine?
    (action_name == 'mine' && controller.controller_name == 'store') or
      (action_name == 'show' && controller.controller_name == 'orders')
  end

  def is_my_cart?
    (action_name == 'my_cart' && controller.controller_name == 'store') or
      (action_name == 'confirm' && controller.controller_name == 'products')
  end

  def is_check_out?
    (action_name == 'new' && controller.controller_name == 'orders') or
      (action_name == 'new' && controller.controller_name == 'payments')
  end

  def format_status(status)
    (Order::STATUS.find {|s| s[1] == status})[0]
  end

  def format_user_status(status)
    (User::STATUS.find {|s| s[1] == status.to_s})[0]
  end

  def format_logistic_type(code)
    (Logistic::TYPE_CODE_FOR_FORM.find {|s| s[1] == code})[0]
  end

  def format_logistic_type_for_alipay(code)
    (Logistic::TYPE_CODE_FOR_ALIPAY.find {|s| s[1] == code})[0]
  end

  def format_boolean(value)
    value ? "<img src='/images/icon/tick.png' alt='true'></img>":"<img src='/images/icon/cross.png' alt='false'></img>"
  end

  def lastest_orders
    return_string = ""
    orders = Order.find(:all,:limit=>10,:order=>'id desc')
    orders.each do |o|
      return_string += "<li>#{o.user.login}的订单:#{format_status o.status}</li>"
    end
    return_string
  end

  def back_url
    request.env["HTTP_REFERER"] || '/store'
  end

  def default_forher_path(sex)
    style = Style.find(:first,:conditions=>["sex = ? and active = 1",sex],:order=>"id desc")
    return if !style
    material = style.materials.first
    return if !material
    #    design = Design.find(:first,:order=>"id desc")
    #    cate = design.categories.first

    "?product[style_id]=#{style.id}&product[material_id]=#{material.id}"
  end

  def default_style_path(sex)
    style = Style.find(:first,:conditions=>["sex = ?",sex],:order=>"id desc")
    return if !style
    material = style.materials.first
    return if !material

    "&product[style_id]=#{style.id}&product[material_id]=#{material.id}"

  end
  
  def format_sex(sex)
    (User::SEX.find {|s| s[1] == sex})[0]
  end

  def foramt_percentage(float)
    "#{(float * 100).to_i}%"
  end
end
